$(function(){
    var ScrollBox = function(){
	this.bar = $('.multiple-container .scroll-bar');
	this.block = $('.multiple-container .scroll-bar a.scroll-block');
	this.target = $('.multiple-container table');
	this.width = $('.multiple-container').width();

	this.blockWidth = 150;
	this.barHeight = 12;

	this.position = {
	    x : 0
	};

	this.enable = true;
    };

    ScrollBox.prototype = {
	init: function(){
	    this.position.x = 0;
	    this.target.css('left',0);
	    this.block.css('left',40);
	    this.bindEvent();
	},
	bindEvent : function(){
	    var _this = this,
	    startMoveX = false;
	    //监听鼠标滚动事件
	    if($.browser.mozilla){
		document.addEventListener('DOMMouseScroll',function(event){
		    _this.onMouseWheel(event);
		},true);
	    }else{
		$(document).bind('mousewheel',function(event){
		    _this.onMouseWheel(event);
		});
	    }

	    //监听滑块移动事件
	    $(this.block).mousedown(function(){
		$(this).addClass('hover');
		return !(startMoveX = true);
	    });

	    $(document).mousemove(function(event){
		if(startMoveX){
		    _this.position.x = event.pageX-_this.blockWidth/2;
		    _this.scroll();
		}
		return false;
	    });
	    $(document).mouseup(function(){
		$(_this.block).removeClass('hover');
		return startMoveX = false;
	    });

	    $(this.bar).click(function(event){
		_this.position.x = event.pageX-_this.blockWidth/2;
		_this.scroll(1);
	    });
	},
	onMouseWheel : function(event){
	    if(!this.enable){
		return;
	    }
	    
	    var direction = $.browser.mozilla?-event.detail:event.originalEvent.wheelDelta,step=30;
	    if(direction == -Math.abs(direction)){
		//向下滚动
		if(this.position.x < (this.width-this.blockWidth)){
		    var lastDistance = this.width-this.blockWidth-this.position.x-80;
		    this.position.x += lastDistance<step?lastDistance:step;
		}
	    }else{
		//向上滚动
		if(this.position.x > 0){
		    this.position.x -= this.position.x<step?this.position.x:step;
		}
	    }

	    this.scroll();
	},
	scroll : function(animate){
	    this.width = $('.multiple-container').width();
	    this.targetWidth = $('.multiple-container table').width();

	    if(this.targetWidth > this.width){
		var leftDistance = this.width - this.blockWidth - 80,
		moveDistance = this.targetWidth-this.width - 80;

		if(this.position.x < 0){
		    this.position.x = 0;
		}

		if(this.position.x > leftDistance){
		    this.position.x = leftDistance;
		}

		$(this.block).css('left',this.position.x+40+'px');

		var offsetLeft = -(this.position.x/leftDistance)*moveDistance + 'px';

		if(animate||false){
		    this.target.animate({
			'left':offsetLeft
		    }, 300);
		}else{
		    this.target.css('left',offsetLeft);
		}
	    }
	}
    };

    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

    $('a').live('click',function(){
	$(this).trigger('blur');
    });

    $('li.language a').click(function(){
	window.location.href = Z.url('/frontend/language?lang='+($(this).html()=='ENGLISH'?'en':'zh'));
    });

    //----menu---start
    var globalTimeout;
    $('ul.main-navigator li').bind('mouseenter',function(){
	var ul = $(this).children('ul'),a = $(this).children('a');
	globalTimeout = window.setTimeout(function(){
	    $(ul).show();
	    $(a).addClass('hover');
	},100);
    });
    $('ul.main-navigator li').bind('mouseleave',function(){
	window.clearTimeout(globalTimeout);
	$(this).children('ul').hide();
	$(this).children('a').removeClass('hover');
    });
    //----menu---end

    Z.innerTip($('.search input'), LANG=='en'?'search':'搜索');

    //build select
    var onSelectClose = function(e){
	if(!$(e.target).is('.select a')){
	    $('.select ul').hide();
	    $(document).unbind('click', onSelectClose);
	}
    },
    onSelectOpen = function(){
	$(this).next().show();
	$(document).bind('click',onSelectClose);
    //return false;
    },
    onSelectChange = function(){
	$(this).parent().parent().find('a.active').removeClass('active');
	$(this).addClass('active');
	$(this).parents('.select').children('a').html($(this).html());
	$(this).parent().parent().hide();
    //return false;
    };

    $('.select a').bind('click',onSelectOpen);
    $('.select ul a').bind('click',onSelectChange);
    $('.select ul a:first').trigger('click');

    var scrollBox = new ScrollBox();
    var multipleAfter = function(data){
	$('.single-container').hide();
	$('.multiple-container').show();

	if($('a.light-box').length > 0){
	    $('a.light-box').click(function(){
		if($(this).siblings('ul').length >0){
		    $(this).siblings('ul').find('a:eq(0)').trigger('click');
		}else{
		    var album = Z.parseInt($(this).attr('data')),
		    td=$(this).parent();
		    Z.ajax({
			url: Z.url('/photo/records/'+album),
			success: function(data){
			    var html = ['<ul style="display:none;">'];
			    $(data.records).each(function(i,item){
				html.push('<li>');
				html.push('<a href="javascript:void(0);" data="'+Z.url('/assets/uploads/'+item.file_name)+'">');
				html.push('</a></li>');
			    });
			    html.push('</ul>');
			    $(td).append(html.join(''));
			    new Z.LightBox($(td).find('ul a'),function(a){
				return $(a).attr('data');
			    },function(){
				return $(td).find('.description').html();
			    },{
				open : function(){
				    scrollBox.enable = false;
				},
				close : function(){
				    scrollBox.enable = true;
				}
			    });
			    $(td).find('ul a:eq(0)').trigger('click');
			}
		    });
		}
	    });
	}

	scrollBox.init();
	
	if($('.pagination .next').length > 0){
	    if($('.pagination .next a').length > 0){
		$('.pagination .next a').html('<img src="/assets/img/next.jpg" alt="下一页"/>');
	    }else{
		$('.pagination .next').html('');
	    }
	}

	if($('.pagination .previous').length > 0){
	    if($('.pagination .previous a').length > 0){
		$('.pagination .previous a').html('<img src="/assets/img/previous.jpg" alt="上一页"/>');
	    }else{
		$('.pagination .previous').html('');
	    }
	}
    },
    singleAfter = function(){
	$('.single-container').show();
	$('.multiple-container').hide();
	if($('.z-circulate-show').length > 0){
	    new Z.CirculateShow($('.z-circulate-show'),500,360);
	}

	$('.content :image').attr('onload','window.alignImage(this,480,0);');

	new Z.ScrollBox({
	    target: $('.single-container .text .content'),
	    height: 260,
	    width: 500,
	    verticalBlockHeight: 40,
	    position : {
		top:0
	    }
	});
    },
    templateMethods = {
	shortContent: function(c,l){
	    return Z.substr(Z.trimHtmpTags(c),0,(l||200));
	},
	thumb : function(f){
	    return (f||'').replace(/\.(jpg|gif|png|bmp)/i,'_thumb.$1')
	},
	nl2br: function(s){
	    return s.replace(/\n/g,'<br/>');
	},
	gTitle: function(s,f){
	    return (f||false)?s.split('@')[0]:s.replace('@','<br/>');
	}
    };

    /* - - - - - - - - - - - - - - 飘逸的分割线- - - - - - - - - - - - - - - -*/

    var projectAjaxFill = new Z.AjaxFill({
	url: Z.url('/project/records'),
	templates : {
	    list: $('#projects-template'),
	    methods: templateMethods
	},
	containers : {
	    list : $('.multiple-container table tr'),
	    pageLinks : $('.page-links'),
	    loading: $('.multiple-container')
	},
	after: multipleAfter,
	data: {
	    pageSize: 10
	}
    });

    $('a.projects-type').live('click',function(){
	projectAjaxFill.request({
	    type:Z.parseInt($(this).attr('data')),
	    year:0,
	    area:0,
	    title: ''
	});
	return false;
    });

    $('a.projects-year').live('click',function(){
	var data = {
	    type:0,
	    area:0,
	    title: ''
	};
	if($(this).attr('data') == '2002'){
	    data.year = 2002;
	}else{
	    data.year = Z.parseInt($(this).html());
	}
	projectAjaxFill.request(data);
	return false;
    });

    $('a.projects-area').live('click',function(){
	projectAjaxFill.request({
	    area:Z.parseInt($(this).attr('data')),
	    type:0,
	    year:0,
	    title: ''
	});
	return false;
    });

    $('.search a').click(function(){
	projectAjaxFill.request({
	    title: $(this).next().val(),
	    area:0,
	    type:0,
	    year:0
	});
	return false;
    });

    /* - - - - - - - - - - - - - - 飘逸的分割线- - - - - - - - - - - - - - - -*/

    var entriesAjaxFill = new Z.AjaxFill({
	url: Z.url('/entry/records'),
	templates : {
	    list: $('#entries-template'),
	    methods: templateMethods
	},
	containers : {
	    list : $('.multiple-container table tr'),
	    pageLinks : $('.multiple-container .page-links'),
	    loading: $('.multiple-container')
	},
	after: multipleAfter,
	data: {
	    pageSize: 10
	}
    });

    $('a.entries').live('click',function(){
	entriesAjaxFill.request({
	    type:Z.parseInt($(this).attr('data'))
	});
	return false;
    });
    /* - - - - - - - - - - - - - - 飘逸的分割线- - - - - - - - - - - - - - - -*/
    var homeAjaxFill = new Z.AjaxFill({
	url: Z.url('/home/records'),
	templates : {
	    list: $('#home-template'),
	    methods: templateMethods
	},
	containers : {
	    list : $('.multiple-container table tr'),
	    pageLinks : $(' .page-links'),
	    loading: $('.multiple-container')
	},
	after: multipleAfter,
	data: {
	    pageSize: 10
	}
    });

    $('a.home').live('click',function(){
	homeAjaxFill.request();
	return false;
    }).trigger('click');

    /* - - - - - - - - - - - - - - - - 飘逸的分割线- - - - - - - - - - - - - - - - -*/

    var entryAjaxFill = new Z.AjaxFill({
	url: Z.url('/entry/find'),
	templates : {
	    list: $('#entry-template'),
	    methods: templateMethods
	},
	containers : {
	    list : $('.single-container'),
	    loading: $('.single-container')
	},
	after: singleAfter,
	data: {
	    pageSize: 10
	}
    });

    $('a.entry').live('click',function(){
	var data = $(this).attr('data');
	entryAjaxFill.setParameter('url',Z.url('/entry/find/'+data)).request();
	return false;
    });

    /* - - - - - - - - - - - - - - - - 飘逸的分割线- - - - - - - - - - - - - - - - -*/

    $('a.message').click(function(){
	$('.single-container').show();
	$('.multiple-container').hide();
	$('.single-container').html($('#message-template').html());
	return false;
    });

    $('.single-container form').live('submit',function(){
	Z.ajax({
	    url: $(this).attr('action'),
	    data: $(this).serialize(),
	    type: $(this).attr('method')
	});
	return false;
    });

    var magazineAjaxFill = new Z.AjaxFill({
	url: Z.url('/entry/records'),
	templates : {
	    list: $('#projects-template'),
	    methods: templateMethods
	},
	containers : {
	    list : $('.multiple-container table tr'),
	    pageLinks : $('.multiple-container .page-links'),
	    loading: $('.multiple-container')
	},
	after: multipleAfter,
	data: {
	    pageSize: 10
	}
    });

    $('a.magazine').live('click',function(){
	magazineAjaxFill.request({
	    type:Z.parseInt($(this).attr('data'))
	});
	return false;
    });

    /* - - - - - - - - - - - - - - - - 飘逸的分割线- - - - - - - - - - - - - - - - -*/
    $('a.job').live('click',function(){
	Z.ajax({
	    url :  Z.url('/job/records'),
	    data : {
		pageSize :0
	    },
	    success : function(data){
		$('.single-container').show().html('');
		$('.multiple-container').hide();
		$('#job-template').tmpl(data).appendTo($('.single-container'));

		new Z.ScrollBox({
		    target: $('.single-container .text .content'),
		    height: 260,
		    width: 500,
		    verticalBlockHeight: 40,
		    position : {
			top:0
		    }
		});
	    }
	});
    });

    $('a.job-show').live('click',function(){
	Z.ajax({
	    url : Z.url('/job/find'),
	    data : {
		id : Z.parseInt($(this).attr('data'))
		},
	    success : function(data){
		Z.alert({
		    width   : 700,
		    title   : data.position,
		    content : $('#job-show-template').tmpl(data,templateMethods)
		});
	    }
	});
    });

    /* - - - - - - - - - - - - - - - - 飘逸的分割线- - - - - - - - - - - - - - - - -*/
    if($(document).height() > 740){
	$('.multiple-container,.single-container').css('top', '55px');
	$('.search').css('top','489px');
	$('.select').css('top','519px');
    }
});